<?php

namespace app\model\v1\setup;

use app\model\order\StoreOrder;
use app\model\user\User;
use app\model\v1\gift\GiftCard;
use app\model\v1\gift\GiftCardCate;
use app\model\v1\gift\GiftCardUser;
use app\model\v1\gift\RecruitLanguage;
use app\services\order\StoreOrderCreateServices;
use app\services\user\UserAddressServices;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\facade\Db;
use think\Model;


class SetupRole extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    protected $table = 'eb_setup_role';




    public function getRoleName($ids = '')
    {
        $data = self::whereIn('id', $ids)->column('group_concat(name) as name');
        return $data[0] ?? '';
    }

    public static function role_list($param = [])
    {
        $data['data'] = self::where("is_del", '0')
            ->where("type", 7)
            ->where(function ($query) use ($param){
                if(!empty($param["name"])) $query->where('name','like','%'.$param['name'].'%');
            })
            ->page($param['page'],$param['limit'])
            ->select()->toArray();

        $data['count'] = self::where("is_del", '0')
            ->where("type", 7)
            ->where(function ($query) use ($param){
                if(!empty($param["name"])) $query->where('name','like','%'.$param['name'].'%');
            })
            ->count();

        return $data;
    }


    public static function role_add($param = [])
    {
        $find = self::where('name',$param['name'])->where("is_del", 0)->find();
        if (!empty($find)) Throw new Exception('角色不能重复');

        $add = [
            "name"          => $param["name"],
            "limits_id"     => $param["limits_id"] ?? '',
            "limit_id"      => $param["limit_id"] ?? '',
            "limits_data"   => $param["limits_data"] ?? '',
            "type"          => 7,   //平台角色
            "ctime"         => date('Y-m-d H:i:s'),
        ];

        return self::insert($add);
    }


    public static function role_up($param = [])
    {
        //$update["update_time"] = time();  //更新时间
        if(!empty($param["name"]))             $update["name"] = $param["name"];
        if(!empty($param["limits_id"]))        $update["limits_id"] = $param["limits_id"];
        if(!empty($param["limit_id"]))         $update["limit_id"] = $param["limit_id"];
        if(!empty($param["limits_data"]))      $update["limits_data"] = $param["limits_data"];

        return self::where("id", $param["id"])->update($update);
    }




}